
import store from '../store/index'

export function getToken() {
    return store.state.token;
}
/**
 * 根据角色id查询处所有的权限
 * @param {Array} roleIds 
 * @returns 
 */
export function getPermissionByRoleIds(roleIds) {
    let roles = store.state.roles
    if (roleIds == null
        || roleIds.length <= 0
        || roles == null
        || roles.length <= 0) {
        return false;
    }
    roles = roles.filter((v) => {
        return roleIds.includes(v.roleId)
    });
    if (roles == null
        || roles.length <= 0) {
        return false;
    }
    const permissions = []
    roles.forEach((v) => {
        permissions.push(...v.permissions);
        if (v.permissions != null) {
            v.permissions.forEach((vv) => {
                permissions.push(...vv.permissions);
            })
        }
    })

    return permissions;
}

/**
 * 根据角色id 判断 改角色是否具备某个权限
 * @param {Array} roleIds 
 * @param {Number} permissionId 
 */
export default function checkPermission(roleIds, url) {
    let permissons = getPermissionByRoleIds(roleIds);
    permissons = permissons.filter((v) => {
        return v.url == url
    });
    return permissons != null && permissons.length > 0
}